package com.vitabuybuy.tv.ui.mine.order.after

import android.os.Bundle
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.library.common.base.BaseActivity
import com.library.common.base.MessageModel
import com.library.common.net.loadingtype.LoadingType
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityAfterSaleBinding
import com.vitabuybuy.tv.ui.mine.order.after.adapter.AfterSaleListAdapter
import com.vitabuybuy.tv.ui.mine.order.after.viewmodel.AfterSaleViewModel
import kotlinx.android.synthetic.main.activity_after_sale.*

class AfterSaleActivity : BaseActivity<AfterSaleViewModel, ActivityAfterSaleBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_after_sale
    private var pageNumber = 1

    private val afterSaleListAdapter by lazy { AfterSaleListAdapter() }


    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.after_sale_2))
        mBinding.mViewModel = viewModel
        listView.apply {
            adapter = afterSaleListAdapter
            layoutManager = LinearLayoutManager(mContext)
        }
    }

    override fun observeData() {
        viewModel.getOrderList(pageNumber)
        viewModel.orderlist.observe(this, Observer {
            refresh.onLoad(it.size)
            if (pageNumber == 1) {
                afterSaleListAdapter.setNewData(it)
            } else {
                afterSaleListAdapter.addData(it)
            }
            if (afterSaleListAdapter.mData.isEmpty()) {
                viewModel.statusView.set(LoadingType.SHOW_EMPTY)
            }else{
                viewModel.statusView.set(LoadingType.SHOW_CONTENT)
            }
        })
    }


    override fun eventHandling() {
        refresh.setOnRefreshListener(object : RefreshListenerAdapter() {
            override fun onLoadMore(refreshLayout: TwinklingRefreshLayout?) {
                super.onLoadMore(refreshLayout)
                pageNumber++
                viewModel.getOrderList(pageNumber)
            }

            override fun onRefresh(refreshLayout: TwinklingRefreshLayout?) {
                super.onRefresh(refreshLayout)
                pageNumber = 1
                viewModel.getOrderList(pageNumber)
            }
        })
        afterSaleListAdapter.onClickViewDetail = {
            AfterSaleDetailActivity.openActivity(mContext,it)
        }
    }

    companion object {
        fun openActivity(mContext: BaseActivity<*, *>) {
            mContext.startActivity(null, AfterSaleActivity::class.java)
        }
    }
}
